package com.gopay.scheduler.job;

import com.gopay.common.constants.acps.AsyncAcctDtlState;
import com.gopay.common.constants.dic.GopayGlobalVariablesConstant;
import com.gopay.common.dss.tools.FuseTool;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.GopayBaseJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhaojt on 2017/11/15.
 *
 * 异步账务流水堆积告警
 */
public class AsynAcctDtlAlarmJob extends GopayBaseJob {


    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;

    @Resource(name = "fuseTool")
    private FuseTool fuseTool;


    private long shortTimeIntervalTime;

    private int shortTimeUnfreezeMaxSize;

    private long longTimeIntervalTime;

    private int longTimeUnfreezeMaxSize;


    @Override
    protected void doJobInternal() {

        try {
            logger.info("异步账务流水堆积告警任务...");

            int longTimeCount = executeQuery(new Date(System.currentTimeMillis() - longTimeIntervalTime));

            logger.error("longTimeCount = " + longTimeCount + ",longTimeUnfreezeMaxSize  = " + longTimeUnfreezeMaxSize);
            if (longTimeCount > longTimeUnfreezeMaxSize) {
                logger.error("触发长时阈值执行报警... ");
                fuseTool.fuse(GopayGlobalVariablesConstant.ASYN_ACCT_STACK_SWITCH, GopayGlobalVariablesConstant.ASYN_ACCT_STACK_FUSE_MOBILE, 1);
            }

            int shortTimeCount = executeQuery(new Date(System.currentTimeMillis() - shortTimeIntervalTime));

            logger.error("shortTimeCount = " + shortTimeCount + ",shortTimeUnfreezeMaxSize  = " + shortTimeUnfreezeMaxSize);
            if (shortTimeCount > shortTimeUnfreezeMaxSize) {
                logger.error("触发短时阈值执行报警...");
                fuseTool.fuse(GopayGlobalVariablesConstant.ASYN_ACCT_STACK_SWITCH, GopayGlobalVariablesConstant.ASYN_ACCT_STACK_FUSE_MOBILE, 1);
            }

            logger.info("异步账务流水堆积告警任务结束");

        }catch (Exception e){
            logger.error("异步账务流水堆积告警任务异常", e);
        }
    }


    private int executeQuery(Date endTime){

        StringBuilder sql = new StringBuilder();

        sql.append(" select count(*) from acps_async_acct_dtl t");
        sql.append(" where t.state = :state");
        sql.append(" and t.create_ts <= :date");

        Map<String, Object> param = new HashMap<>();
        param.put("date", endTime);
        param.put("state", AsyncAcctDtlState.UNDO.value);

        int count = commonQueryDAO.findCountBySql(sql.toString(), param);


        return count;
    }

    public long getShortTimeIntervalTime() {
        return shortTimeIntervalTime;
    }

    public void setShortTimeIntervalTime(long shortTimeIntervalTime) {
        this.shortTimeIntervalTime = shortTimeIntervalTime;
    }

    public int getShortTimeUnfreezeMaxSize() {
        return shortTimeUnfreezeMaxSize;
    }

    public void setShortTimeUnfreezeMaxSize(int shortTimeUnfreezeMaxSize) {
        this.shortTimeUnfreezeMaxSize = shortTimeUnfreezeMaxSize;
    }

    public long getLongTimeIntervalTime() {
        return longTimeIntervalTime;
    }

    public void setLongTimeIntervalTime(long longTimeIntervalTime) {
        this.longTimeIntervalTime = longTimeIntervalTime;
    }

    public int getLongTimeUnfreezeMaxSize() {
        return longTimeUnfreezeMaxSize;
    }

    public void setLongTimeUnfreezeMaxSize(int longTimeUnfreezeMaxSize) {
        this.longTimeUnfreezeMaxSize = longTimeUnfreezeMaxSize;
    }
}
